/**
 * 系统设置 员工管理
 */
<template>
  <div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">

      <el-form-item label="所属科室：">
        <!-- 下拉框 -->
        <el-select size="small" v-model="formInline.officeId" placeholder="所属科室" @change="selectChange">
          <el-option  label="所有科室" value=""></el-option>          
          <el-option v-for="office in officeList" :label="office.name" :value="office.id" :key="office.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工姓名：">
        <!-- 搜索框 -->
        <el-input size="small" v-model="formInline.name" placeholder="输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>

    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column sortable prop="id" label="序号" width="80">
      </el-table-column>
      <el-table-column sortable prop="number" label="工号" width="80">
      </el-table-column>
      <el-table-column sortable prop="name" label="名称" width="80">
      </el-table-column>
      <el-table-column sortable prop="sex" label="性别" width="80">
      </el-table-column>
      <el-table-column sortable prop="age" label="年龄" width="80">
      </el-table-column>
      <el-table-column sortable prop="phone" label="手机号码" width="100">
      </el-table-column>
      <el-table-column sortable prop="clinicName" label="所属诊所" width="100">
      </el-table-column>
      <el-table-column sortable prop="officeName" label="所属科室" width="100">
      </el-table-column>
      <el-table-column sortable prop="roleName" label="角色" width="100">
      </el-table-column>
      <el-table-column sortable prop="createTime" label="创建时间" width="180">        
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteOper(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="员工编号" prop="number">
          <el-input-number :min="1000" :max="9999" size="small" v-model="editForm.number" auto-complete="off" placeholder="请输入员工编号"></el-input-number>
        </el-form-item>
        <el-form-item label="员工姓名" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入员工姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select size="small" v-model="editForm.sex" auto-complete="off" placeholder="请输入员工性别">
            <el-option value="男"></el-option>
            <el-option value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number :min="1" :max="200" size="small" v-model="editForm.age" auto-complete="off" placeholder="请输入员工年龄"></el-input-number>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input size="small" v-model="editForm.phone" auto-complete="off" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        <el-form-item label="所属诊所" prop="clinicId">
          <el-select size="small" v-model="editForm.clinicId" placeholder="所属诊所">                     
            <el-option v-for="clinic in clinicList" :label="clinic.name" :value="clinic.id" :key="clinic.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属科室" prop="officeId">
          <el-select size="small" v-model="editForm.officeId" placeholder="所属科室">                     
            <el-option v-for="office in officeList" :label="office.name" :value="office.id" :key="office.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select size="small" v-model="editForm.roleId" placeholder="角色">                     
            <el-option v-for="role in roleList" :label="role.name" :value="role.id" :key="role.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" type="password" v-model="editForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <!-- 如果是添加则显示添加按钮，点击事件对应添加方法 -->
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitInsertForm('editForm')" v-if="title=='添加'">添加</el-button>
        <!-- 如果是修改则显示修改按钮，点击事件对应修改方法 -->
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitUpdateForm('editForm')" v-if="title=='修改'">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入组件
import Pagination from '../../../components/Pagination'
import {validatePhone , validateIdNo}  from "../../../utils/validate";
import axios from 'axios'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      // 编辑页数据
      editForm: {
        id:'',
        number:'',
        name:'', 
        sex:'',
        age:'', 
        phone:'', 
        clinicId:'', 
        officeId:'', 
        roleId:'',
        password:'',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证 （暂时还没有写）
      rules: {
        number: [{ required: true, message: '请输入员工编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        sex:[{required: true, len:1,message: '请输入员工性别', trigger: 'blur'}],
        age:[{required: true,type:'number', message: '请输入员工年龄', trigger: 'blur'}],
        phone:[{ required: true, message: "请输入正确的11位电话号码", trigger: "blur" },{validator: validatePhone, trigger: "blur"}],
        clinicId: [{ required: true, message: '所属诊所', trigger: 'blur' }],
        officeId: [{ required: true, message: '所属科室', trigger: 'blur' }],
        roleId: [{ required: true, message: '角色', trigger: 'blur' }],
        password: [{ required: true, message: '请输入员工密码', trigger: 'blur' }],
      },
      // 分页，查询信息
      formInline: {
        page: 1,
        limit: 10,
        officeId:'',
        name: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门（没用）
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      // 诊所列表
      clinicList: [] ,
      // 科室列表
      officeList: [] , 
      // 角色列表
      roleList: [] ,   
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 创建完毕获取员工列表
   * 创建完毕获取诊所列表
   * 创建完毕获取科室列表
   * 创建完毕获取角色列表
   */
  created() {
    this.getdata(this.formInline);
    this.getClinicList();
    this.getOfficeList();
    this.getRoleList();    
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取员工列表
    getdata(parameter) {
      this.loading = true      
      axios.post('/api/employee/selectAllEmployee?pageNum='+parameter.page+'&pageSize='+parameter.limit,{'officeId':parameter.officeId,'name':parameter.name})
        .then(res => {
          this.loading = false     
          console.log('结果是',res)   
          // listData赋值  
          this.listData = res.data.data.list
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.data.data.total          
        })
        .catch(err => {
          this.loading = false
          this.$message.error('员工列表加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id= row.id
        this.editForm.number= row.number
        this.editForm.name= row.name
        this.editForm.sex= row.sex
        this.editForm.age= row.age 
        this.editForm.phone= row.phone 
        this.editForm.clinicId= row.clinicId 
        this.editForm.officeId= row.officeId 
        this.editForm.roleId= row.roleId
      } else {
        this.title = '添加'
        this.editForm.id= ''
        this.editForm.number= ''
        this.editForm.name= ''
        this.editForm.sex= ''
        this.editForm.age= '' 
        this.editForm.phone= '' 
        this.editForm.clinicId= '' 
        this.editForm.officeId= '' 
        this.editForm.roleId= ''
      }
    },
    // 新增弹窗的保存方法
    submitInsertForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/employee/insertOneEmployee',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              console.log('插入的结果是',res)
              if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '员工保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('员工保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 更新弹窗的保存方法
    submitUpdateForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/employee/updateEmployeeById',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              console.log('更新的结果是',res)
              if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '员工更新成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('员工更新失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除员工
    deleteOper(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          axios.post('/api/employee/deleteEmployeeById?id='+row.id)
          // deptDelete(row.deptId)
            .then(res => {
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '员工已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('员工删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },
    // 获取诊所列表
    getClinicList(){
        axios.post('/api/clinic/selectAllClinic?pageNum=1&pageSize=1000',{})
        .then(res => {
          this.loading = false     
          console.log('结果是',res)     
          this.clinicList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('诊所加载失败，请稍后再试！')
        })
    },
    // 获取科室列表
    getOfficeList(){
        axios.post('/api/office/selectAllOffice?pageNum=1&pageSize=1000',{})
        .then(res => {
          this.loading = false 
          this.officeList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('科室加载失败，请稍后再试！')
        })
    },
    // 获取角色列表
    getRoleList(){
        axios.post('/api/role/selectAllRole?pageNum=1&pageSize=1000',{})
        .then(res => {
          this.loading = false
          this.roleList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('角色加载失败，请稍后再试！')
        })
    },
    // 科室选择框改变后调用
    selectChange(){
      this.formInline.page=1
      this.getdata(this.formInline)      
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 